import { createSkeletonComponent } from "../component/stokelen/index.js"
import { createContent } from "../component/content/index.js"
import config from "../util/config.js";
import { getInfo } from "./api-user.js";
import collect from "../util/collect.js";

// var defaultActive = 0; // 默认激活项
let active_ = localStorage.getItem("active");
if (active_) config.active = active_

function get_page(index) {
    if (index <= collect.length - 1) {
        return collect[index]
    }
    return null
}

async function init_page(active_ = undefined) {
    const active = active_ || config.active
    const data = await getInfo(1)

    return {
        ...config,

        ...data[0],
        active,
    }
}

$(document).ready(async function () {
    // 定义内容函数，返回空内容

    // 调用 createSkeletonComponent 函数，并传递默认导航项和激活项
    createSkeletonComponent('body', createContent(get_page(config.active)), await init_page());

    // 处理 nav-item 点击事件
    $(document).on('click', '.nav-item a', async function (event) {
        event.preventDefault(); // 阻止默认链接行为
        const index = $(this).parent().index(); // 获取点击项的索引
        // defaultActive = index; // 更新 defaultActive
        localStorage.setItem("active", index)
        createSkeletonComponent('body', createContent(get_page(index)), await init_page(index)); // 重新调用 createSkeletonComponent
    });
});
